package org.usmile.algorithms.huawei.acm;

import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringJoiner;

// https://blog.csdn.net/qq_34465338/article/details/128713875
public class _信号发射和接收 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int m = sc.nextInt();
        int n = sc.nextInt();

        int[][] anth = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                anth[i][j] = sc.nextInt();
            }
        }

        System.out.println(getResult(anth, m, n));
    }

    public static String getResult(int[][] anth, int m, int n) {
        int[][] ret = new int[m][n];

        // 先处理南向发射信号
        for (int j = 0; j < n; j++) {
            LinkedList<Integer> stack = new LinkedList<>();
            for (int i = 0; i < m; i++) {
                while (stack.size() > 0 && anth[i][j] > stack.getLast()) {
                    ret[i][j] += 1;
                    stack.removeLast();
                }

                if (stack.size() > 0) {
                    if (anth[i][j] == stack.peek()) {
                        stack.removeLast();
                    }
                    ret[i][j] += 1;
                }

                stack.add(anth[i][j]);
            }
        }

        StringJoiner sj = new StringJoiner(" ");
        // 再处理东向发射信号,和上面同理
        for (int i = 0; i < m; i++) {
            LinkedList<Integer> stack = new LinkedList<>();
            for (int j = 0; j < n; j++) {
                while (stack.size() > 0 && anth[i][j] > stack.getLast()) {
                    ret[i][j] += 1;
                    stack.removeLast();
                }

                if (stack.size() > 0) {
                    if (anth[i][j] == stack.peek()) {
                        stack.removeLast();
                    }
                    ret[i][j] += 1;
                }

                stack.add(anth[i][j]);
                sj.add(ret[i][j] + "");
            }
        }

        return m + " " + n + "\n" + sj.toString();
    }
}
